import Vue from 'vue';
import Vuex from 'vuex';
import router from '../router'
import {req} from '../api/axiosFun';

//挂载Vuex
Vue.use(Vuex);

//创建VueX对象
export default new Vuex.Store({
  state: {
    //存放的键值对就是所要管理的状态
    user: {},
    menuList: [],
    selectedKeys: [],
    openKeys: [],
    activeKey: null,
    panes: [],
    defaultMenuUrl: '',
    visibleTodo: false,
    purchasePriorityData:{},
    productWarehouseData:{},
    productDeliverData:{},
    processInspectInitData:{},
    firstInspectInitData:{},
    ArrivalPriorityData: {},
    inspectQX:{},//检验权限
    isLogin: false,
    loginForm: {
      userId: 'yadmin',//账号
      passwd: '123456',//密码
    },
  },
  mutations: {
    //更改登录状态
    setIsLogin(state, isLogin) {
      state.isLogin = isLogin;
    },
    setInspectQX(state, inspectQX) {
      state.inspectQX = inspectQX;
    },
    setVisibleTodo(state, visibleTodo) {
      state.visibleTodo = visibleTodo;
    },
    //保存用户信息到vuex中
    addUser(state, user) {
      state.user = user;
      sessionStorage.setItem('roleCode', user.role_code);
    },
    //菜单list
    setMenuList(state, menuList) {
      state.menuList = menuList;
    },
    //当前选中tab标签key
    setActiveKey(state, activeKey) {
      state.activeKey = activeKey;
    },
    //当前选中菜单key
    setSelectedKeys(state, selectedKeys) {
      state.selectedKeys = selectedKeys;
    },
    //菜单展开项
    setOpenKeys(state, openKeys) {
      state.openKeys = openKeys;
    },
    //tab标签页list
    setPanes(state, panes) {
      state.panes = panes;
    },
    //默认打开的路由菜单url
    setDefaultMenuUrl(state, defaultMenuUrl) {
      state.defaultMenuUrl = defaultMenuUrl;
    },
    //进货检验查询采购入库单优先级、供应商档案、仓库档案
    setPurchasePriorityData(state, purchasePriorityData) {
      state.purchasePriorityData = purchasePriorityData;
    },
    //成品入库检查询部门列表、仓库列表、入库类别、优先级
    setProductWarehouseData(state, productWarehouseData) {
      state.productWarehouseData = productWarehouseData;
    },
    //成品出库检查询客户档案、优先级、部门列表、收货单位
    setProductDeliverData(state, productDeliverData) {
      state.productDeliverData = productDeliverData;
    },
    //到货单检查询
    setArrivalPriorityData(state, productDeliverData) {
      state.ArrivalPriorityData = productDeliverData;
    },
    //过程检验查询车间分类、检验分类
    setProcessInspectInitData(state, processInspectInitData) {
      state.processInspectInitData = processInspectInitData;
    },
    //首检检验
    setFirstInspectInitData(state, firstInspectInitData) {
      state.firstInspectInitData = firstInspectInitData;
    },
  },
  actions: {
    menuList(context, path) {
      return new Promise((resolve, reject) => {
        req("get", "/login/getMenu", {}).then(res => {
          if (res.result === "success") {
            var user = res.user;
            user.queryParamType = res.pd.queryParamType
            //存储用户信息
            context.commit("addUser", user);
            let menuList = res.menuList;
            //存储菜单list
            context.commit("setMenuList", menuList);
            let defaultMenu;
            for (let menu of menuList) {
              if (menu.hasMenu && !menu.subMenu.length && menu.function_url) {
                defaultMenu = menu;
                break;
              } else if (menu.hasMenu && menu.subMenu.length) {
                let flag = false;
                for (let sub of menu.subMenu) {
                  if (sub.hasMenu && sub.function_url) {
                    defaultMenu = sub;
                    flag = true;
                    break;
                  }
                }
                if (flag) {
                  break;
                }
              }
            }
            context.commit("setSelectedKeys", [defaultMenu.function_id]);
            context.commit("setOpenKeys", [defaultMenu.parent_id]);
            let panes = [
              {
                title: defaultMenu.function_name,
                key: defaultMenu.function_id,
                content: defaultMenu,
                closable: false
              }
            ];
            context.commit("setPanes", panes);
            context.commit("setActiveKey", defaultMenu.function_id);
            context.commit("setDefaultMenuUrl", defaultMenu.function_url);
            resolve(defaultMenu.function_url);
            if (path !== defaultMenu.function_url) {
              router.push(defaultMenu.function_url);
            }
          }
        }).catch(error => {
          reject(error)
        })
      });
    },
  },
  getters: {
    //获取用户名
    userName(state) {
      return state.user.user_name;
    },
    //获取用户角色代号
    roleCode(state) {
      return state.user.role_code;
    },
    //获取用户账号
    userCode(state) {
      return state.user.user_code;
    },
    //获取当前选中tab标签key
    getActiveKey(state) {
      return state.activeKey;
    },
    //获取当前选中菜单key
    getSelectedKeys(state) {
      return state.selectedKeys;
    },
    //获取当前菜单list
    getMenuList(state) {
      return state.menuList;
    },
    //采购入库单优先级、供应商档案、仓库档案
    getPurchasePriorityData(state) {
      return state.purchasePriorityData;
    },
    //部门列表、仓库列表、入库类别、优先级
    getProductWarehouseData(state) {
      return state.productWarehouseData;
    }
  }
});
